package com.sali.asyncollection.service.impl.strategy.method7;

import com.google.common.util.concurrent.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: sali
 * @date: 2024/7/21 下午4:01.
 * @version: 1.0
 */
@Component
@Slf4j
public class AsynMethod07 {


    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

    public void execute(Long userId) throws ExecutionException, InterruptedException {
        final ListenableFuture<Integer> listenableFuture = executorService.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                log.info("callable execute...");
                TimeUnit.SECONDS.sleep(1);
//                throw new RuntimeException("我是报错");
                return 1;
            }
        });

        Futures.addCallback(listenableFuture, new FutureCallback<Integer>() {
            @Override
            public void onSuccess(Integer result) {
                //成功执行...
                System.out.println("Get listenable future's result with callback " + result);
            }

            @Override
            public void onFailure(Throwable t) {
                //异常情况处理...
                t.printStackTrace();
            }
        });

        Integer i = listenableFuture.get();
    }

}
